	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_AREA_AD.php' );
	require_once( '../system/mdao/Class_M_AREA_AD_SHOP.php' );
	
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_AREA_AD,$login['staff_auth']);
	
	$isG5 = FALSE ;
	if   ($login['staff_auth'] == 'G-5') {
		$isG5 = TRUE ;
		echo "権限はありません";
		exit;
	}
	
	
	$docsv_import = FALSE;
	$matrix_cd = $_POST["week_cd"];
	//echo $matrix_cd."******";
	
	$csvf = Class_FILE_CSV::get_upload_file();
	
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		$rowall = Class_FILE_CSV::fget_rows($csvf);
		
		if  (!empty($rowall)) {
		
			//--------10地域広告詳細インポートCSV-------開始------------
				$col_heads = array(
					'area_cd'          , 
					'shop_cd'          , //店舗コード',      1),
					''                 , //店舗名',          1),
					'shop_per'         , //店舗部数%',       1),
					''                 , //計算部数',        1),
					''                 , //更新日時',        3),
					''                 , //登録日時',        3),
					''                 , //更新者',          1)
				);
				
				$shopnames =  Class_M_OTHER::get_shop_array( );
				$csvheads  = array();
				$col_index = array();
				foreach ($col_heads as $k => $v) {
					if (!empty($v)) {
						$csvheads[$k]  = $v;
						$col_index[$v] = $k;
					}
				} 
				
				$index_area_cd     = $col_index['area_cd'];  //
				$col_area_cd       = $index_area_cd + 1;
				
				$index_shop_cd     = $col_index['shop_cd'];     //
				$col_shop_cd       = $index_shop_cd + 1;
				
				$key1 = $col_index['area_cd']; //キーです、必ず仕様します
				$key2 = $col_index['shop_cd']; //キーです、必ず仕様します
				
				$err = new Class_ERROR("","");
				$dao = new Class_M_AREA_AD_SHOP;
				//echo "****".$matrix_cd;
				
				$areaad  = $dao->getAreaadByWeek($matrix_cd); 
				//print_r($areaad);
				$ngline  = 0;
				$error_times = 0;
				$error_stop = IMPORT_STOP_MAX_ERROR_TIMES;
				$errtext = array();
				$isng = FALSE;
				
				//$chk = $dao->get_checks();
				$chk =    array(
					"area_cd"       => array( "EXIST" ),
					"shop_cd"       => array( "EXIST" ),
					"shop_per"      => array( "EXIST", "NUM" )
				 );
				$area_arr    = Class_M_OTHER::get_area_array();
				$media_arr   = Class_M_OTHER::get_media_array();
				
				$linecount = count($rowall);
				$percentold = 100;
	 			foreach ($rowall as $line => $row) {
					$isng = FALSE;
					$percent = intval($line/$linecount * 100)."%";
					if  ( $percent != $percentold ) {
						Class_FILE_CSV::write_msg_checking_per( $percent ) ;
					}
					$percentold = $percent;
					//CSV内部重複不可チェック
					foreach ($rowall as $kk=>$vv) {
						if  ($line != $kk) {
							if ($row[$key1] == $vv[$key1] && $row[$key2] == $vv[$key2]){
								$col = $key1 + 1;
								$col2 = $key2 + 1;
								$isng = TRUE;
								$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の{$col}と{$col2}列目組み合わせの値重複不可。";
								$error_times++;
								if ($error_times > $error_stop) {
									break;
								}
							}
						}
					} 
					
					foreach ( $csvheads as $k=>$v ) {
						if (isset( $chk[$v ])) {
							$msg =  $err->check( $rowall[$line][$k], $chk[$v ] ); 
							if (!empty($msg)) {
								$isng = TRUE;
								$col  = $k+1;
								$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
								$error_times++;					
								if ($error_times > $error_stop) {
									break;
								}
							}
						}
					}
					
					//エリア
					if ( !empty($row[$index_area_cd])){
						if (empty($areaad[$row[$index_area_cd]])) {
							$isng = TRUE;
							$error_times++;
							$ngMsg_tmp[$line][] = "{$line}行{$col_area_cd}列目：当週エリアは存在しません。</br>";
						}
					} 
					//店舗
					
					if ( !empty($row[$index_shop_cd])){
						if (empty($shopnames[$row[$index_shop_cd]])) {
							$isng = TRUE;
							$error_times++;
							$ngMsg_tmp[$line][] = "{$line}行{$col_shop_cd}列目：当週エリアは存在しません。</br>";
						}
					} 
					//for (i)lnews1
					if ( $isng ) {
						++$ngline;
						$errtext[$ngline] = $row;
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				
				if ($ngline > 0) {
					$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
					if ($error_times > $error_stop) {
						Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
					
					} else {
						Class_FILE_CSV::write_msg_error( $errHtml );	
					}
				} else {
					//DBへ更新
					try {
						$daodb   = $dao->get_db();
						
						$dao->begin_trans();
						$dao->deleteShop( $matrix_cd ) ;
						$i = 0;
						$percent = "";
						$percentold = "";
						foreach ( $rowall as $k=>$v ) {
							$i++;
							$percent = intval($i/$line * 100)."%";
							if  ( $percent != $percentold ) {
								Class_FILE_CSV::write_msg_doing( $percent ) ;
							}
							$percentold = $percent;
							$values  = array();
							
							//foreach ( $csvheads as $kk => $vv ) {
							//	$values[$vv] = $v[$kk];
							//}
							
							$values["shop_cd"]   = $v[$col_index["shop_cd"]];
							$values["shop_name"] = $shopnames[$values["shop_cd"]];
							$values["shop_per"]  = $v[$col_index["shop_per"]];
							
							//$keyvalues =  $values[$col_heads[$key1]];
							//$where   = sprintf(" matrix_cd =  {$matrix_cd} AND {$col_heads[$key1]} = '%s'", $keyvalues );
							$area_cd = $v[$index_area_cd] ;
							$values["area_ad_shop_id"]   =   $areaad[$area_cd];
							$values["shop_nums"]         =   round( $values["shop_per"] * $area_nums / 100.0 ) ;
							
							//$values["stop_date"]       =   Class_PWD::date_to_int8 ( $values['stop_date'] );
							
							//if ( $dao->exits( $where ) ) {
							//	if (!($dao->editbywhere($where, $values))) {
							//		throw exception("mareaad_import","db更新", AOKI_OTHER, "File not opend");
							//	}
							//} else {
							if ( !( $dao->add($values ) ) ) {
								throw new Exception("mareaad_shop_import db更新");
							}
							//}
						}
						Class_FILE_CSV::write_msg_succed($line, "mareaad");
						$dao->commit_trans();
						
					} catch (Exception $e) {
						$dao->rollback_trans();
						Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
					}
				} 
			//--------10地域広告詳細インポートCSV-------完了------------

		}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
			
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	